package com.itheima.controller.backend;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.constant.RedisConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.common.utils.AliOssUtil;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private AliOssUtil aliOssUtil;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 根据查询条件分页查询体检套餐
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        log.info("根据查询条件分页查询体检套餐，{}", queryPageBean);
        PageResult pageResult = setmealService.findPage(queryPageBean);
        return pageResult;
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findById(Integer id){
        log.info("根据id查询套餐，{}", id);
        try {
            Setmeal setmeal = setmealService.findById(id);
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL, null);
        }
    }

    /**
     * 新增套餐
     * @param checkgroupIds
     * @param setmeal
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestParam List<Integer> checkgroupIds, @RequestBody Setmeal setmeal){
        log.info("新增套餐，{}，{}", checkgroupIds, setmeal);
        try {
            setmealService.add(checkgroupIds, setmeal);
            return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS, null);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_SETMEAL_FAIL, null);
        }
    }

    /**
     * 文件上传
     * @param imgFile
     * @return
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile imgFile){
        log.info("文件上传");
        // 获取原始文件名
        String originalFilename = imgFile.getOriginalFilename();
        // 截取原始文件名后缀
        String extention = originalFilename.substring(originalFilename.lastIndexOf("."));
        // 构造新文件名称
        String objecName = UUID.randomUUID().toString() + extention;
        // 文件上传 获取文件请求路径
        String filePath = null;
        try {
            filePath = aliOssUtil.upload(imgFile.getBytes(), objecName);
        } catch (IOException e) {
            log.error("文件上传失败，{}", e);
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL, null);
        }
        // 文件上传成功后保存在redis集合中
        SetOperations set = redisTemplate.opsForSet();
        set.add(RedisConstant.SETMEAL_PIC_RESOURCES, filePath);
        // 返回结果
        return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, filePath);
    }

    /**
     * 编辑套餐
     * @param checkGroupIds
     * @param setmeal
     * @return
     */
    @PostMapping("/edit")
    public Result edit(@RequestParam List<Integer> checkGroupIds, @RequestBody Setmeal setmeal){
        log.info("编辑套餐,{},{}", checkGroupIds, setmeal);
        try {
            setmealService.edit(checkGroupIds, setmeal);
            return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS, null);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_SETMEAL_FAIL, null);
        }
    }
}
